//
//  ActivityIndicatorManager.m
//  baidu_face
//
//  Created by xiaoka on 2025/3/13.
//

#import "ActivityIndicatorManager.h"

@interface ActivityIndicatorManager ()
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicator;
@property (nonatomic, weak) UIView *targetView;
@end

@implementation ActivityIndicatorManager

+ (instancetype)sharedManager {
    static ActivityIndicatorManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[ActivityIndicatorManager alloc] init];
    });
    return instance;
}

- (void)showIndicator {
    if (@available(iOS 13.0, *)) {
        [self showIndicatorwithStyle:UIActivityIndicatorViewStyleLarge color:[UIColor colorWithRed:217/255.0 green:223/255.0 blue:230/255.0 alpha:1]];
    } else {
        [self showIndicatorwithStyle:UIActivityIndicatorViewStyleWhiteLarge color:[UIColor colorWithRed:217/255.0 green:223/255.0 blue:230/255.0 alpha:1]];
    }
    
}

- (void)showIndicatorInView:(UIView *)view withStyle:(UIActivityIndicatorViewStyle)style {
    [self showIndicatorwithStyle:style color:[UIColor whiteColor]];
}

- (void)showIndicatorwithStyle:(UIActivityIndicatorViewStyle)style color:(UIColor *)color {
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (weakSelf.activityIndicator) {
            [weakSelf.activityIndicator removeFromSuperview];
            weakSelf.activityIndicator = nil;
        }
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        weakSelf.targetView = keyWindow;
        
        weakSelf.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
        weakSelf.activityIndicator.frame = CGRectMake(0, 0, 100, 100);
        weakSelf.activityIndicator.center = [UIApplication sharedApplication].keyWindow.center;
        weakSelf.activityIndicator.backgroundColor = color;
        weakSelf.activityIndicator.layer.cornerRadius = 8;
        [keyWindow addSubview:self.activityIndicator];
        
        // 居中显示
        [weakSelf.activityIndicator.centerXAnchor constraintEqualToAnchor:keyWindow.centerXAnchor].active = YES;
        [weakSelf.activityIndicator.centerYAnchor constraintEqualToAnchor:keyWindow.centerYAnchor].active = YES;
        
        [weakSelf.activityIndicator startAnimating];
        
        // 淡入动画
        [UIView animateWithDuration:0.3 animations:^{
            weakSelf.activityIndicator.alpha = 1;
        }];
    });
}

- (void)hideIndicator {
    dispatch_async(dispatch_get_main_queue(), ^{
        if (self.activityIndicator) {
            // 淡出动画
            [UIView animateWithDuration:0.3 animations:^{
                self.activityIndicator.alpha = 0;
            } completion:^(BOOL finished) {
                [self.activityIndicator stopAnimating];
                [self.activityIndicator removeFromSuperview];
                self.activityIndicator = nil;
            }];
        }
    });
}

@end
